﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AGRTraining.za.co.agrtraining.model;

namespace AGRTraining.za.co.agrtraining.repository.Impl
{
    public class ClientDAO : IDAO<Client>
    {
        private AGRModelContainer agrDb = new AGRModelContainer();

        public void persist(Client entity)
        {
            agrDb.Clients.AddObject(entity);
        }

        public void remove(Client entity)
        {
            agrDb.Clients.DeleteObject(entity);
        }

        public void removeById(string code)
        {
            foreach (Client client in agrDb.Clients.Where(s => s.ClientCode == code))
            {
                agrDb.Clients.DeleteObject(client);
            }
        }

        public void merge(Client entity)
        {
            foreach (Client client in agrDb.Clients.Where(s => s.ClientCode == entity.ClientCode))
            {
                client.ClientDescription = entity.ClientDescription;
                client.ClientEmail = entity.ClientEmail;
                client.ClientFaxNumber = entity.ClientFaxNumber;
                client.ClientName = entity.ClientName;
                client.ClientPhysicalAddress = entity.ClientPhysicalAddress;
                client.ClientPostalAddress = entity.ClientPostalAddress;
                client.ClientTelNumber = entity.ClientTelNumber;
            }

            //Select ALL
            //var students = (from s in agrDb.Students
            //                where s.StudentCode == "dsfsdf"
            //                select s);

            //Select Certain Columns
            //var students2 = (from s in agrDb.Students
            //                select new
            //                {
            //                    s.StudentCourses,
            //                    s.StudentDisability
            //                });

            //Order rows
            //var orderedListByCourses = students.OrderBy(s => s.StudentCourses);

        }

        public Client findById(string code)
        {
            Client client = new Client();

            foreach (Client cli in agrDb.Clients.Where(s => s.ClientCode == code))
            {
                client = cli;
            }

            return client;
        }

        public IEnumerable<Client> findAll()
        {
            var clients = (from cli in agrDb.Clients
                            select cli);

            return clients;
        }

        public IEnumerable<Client> findInRange(int firstResult, int maxResuslts)
        {
            var clients = (from cli in agrDb.Clients
                            select cli);

            return clients.Skip(firstResult).Take(maxResuslts);
        }

        public long count()
        {
            var clients = (from cli in agrDb.Clients
                            select cli).ToList();

            return clients.Count();
        }

        public void commit()
        {
            try
            {
                agrDb.SaveChanges();
            }
            catch
            {

            }
        }
    }
}